using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Core;
using Core.GameObjects;
using SdlDotNet.Core;
using SdlDotNet.Graphics;
using SpaceTrader.Core;

namespace SpaceTrader.WinUI
{
    public partial class GameWindow : Form, IController
    {
        protected ConsoleWindow Console;
        private int ElapsedTicks = 0;
        protected Game mGame;
        private Player Player;

        protected Surface RenderingSurface;
        private float Zoom = 1.0f;

        public GameWindow(Game GameToControl)
        {
            Game = GameToControl;
            Game.AddController(this);

            Console = new ConsoleWindow();
            Game.AddController(Console);

            InitializeComponent();
            InitializeSurface();

            SdlDotNet.Core.Events.VideoResize += Events_Resize;

            InitializeGame();
        }

        #region IController Members

        public Game Game
        {
            get { return mGame; }
            set { mGame = value; }
        }

        public void Render()
        {
            RotationMonitor.Text = "Rotation: " + Game.Objects[0].Rotation;
            if (ElapsedTicks >= 2)
            {
                ElapsedTicks = 0;

                RenderingSurface.Fill(Color.Black);
                foreach (Player Obj in Game.Objects)
                {
                    Surface ObjSurf = Obj.Render();
                    ObjSurf = ObjSurf.CreateScaledSurface(Zoom);

                    Point BlitPos = Obj.Position;
                    BlitPos.Offset((ObjSurf.Width/2)*-1, (ObjSurf.Height/2)*-1);

                    Vector ScaledPos = new Vector(BlitPos)*new Vector(Zoom, Zoom, 0f);
                    RenderingSurface.Blit(ObjSurf, ScaledPos.Point);
                }

                surfaceControl1.Blit(RenderingSurface);
            }
            else
                ElapsedTicks += 1;
        }

        #endregion

        private void InitializeGame()
        {
            Player = new Player(RenderingSurface.Width/2, RenderingSurface.Height/2, "Player");
            Player.Thrust = 5;
            Game.AddObject(Player);

            Logger.Instance().Log(String.Format("Player created at {0}.", Player.Position), LogLevel.Info);
        }

        private void GameWindow_Load(object sender, EventArgs e)
        {
            Thread thread = new Thread(SdlDotNet.Core.Events.Run);
            thread.IsBackground = true;
            thread.Name = "SDL.NET";
            thread.Priority = ThreadPriority.Normal;
            thread.Start();
        }

        private void Events_Resize(object sender, VideoResizeEventArgs e)
        {
            InitializeSurface();
        }

        private void InitializeSurface()
        {
            RenderingSurface = new Surface(surfaceControl1.Width, surfaceControl1.Height);
        }

        private void ToggleConsole()
        {
            if (Console.Visible == false)
                Console.Show();
            else
                Console.Hide();
        }

        private void GameWindow_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    ToggleConsole();
                    break;

                case Keys.Up:
                    Player.Throttle = 1.0f;
                    break;

                case Keys.Left:
                    if (Player.Rotation - 5 > -1)
                        Player.Rotation -= 5;
                    else
                        Player.Rotation = 355;
                    break;

                case Keys.Right:
                    if (Player.Rotation + 5 < 360)
                        Player.Rotation += 5;
                    else
                        Player.Rotation = 0;
                    break;
            }
        }

        private void GameWindow_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:
                    Player.Throttle = 0.0f;
                    break;
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Zoom = 0.25f;
            toolStripDropDownButton1.Text = "Zoom: 25%";
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            Zoom = 0.5f;
            toolStripDropDownButton1.Text = "Zoom: 50%";
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            Zoom = 0.75f;
            toolStripDropDownButton1.Text = "Zoom: 75%";
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            Zoom = 1.0f;
            toolStripDropDownButton1.Text = "Zoom: 100%";
        }
    }
}